package com.xxg.renrenorder.strategy;

import com.xxg.renrencommon.domain.filedenum.orderenum.OrderStatusEnum;
import com.xxg.renrencommon.exception.OrderManagerException;
import com.xxg.renrencommon.exception.exceptionenum.OrderExceptionCodeCodeEnum;
import com.xxg.renrenorder.dao.bean.AfterSalesOrderInfo;
import com.xxg.renrenorder.dao.bean.OrderMasterInfo;

import java.util.Date;

/**
 * 维修
 *
 * @author szj
 * @date 2021/12/17
 */
public class RepairProductStrategy extends AbstractAfterSalesStrategy{
    @Override
    public void handlerAfterSalesOrderInfo(AfterSalesOrderInfo afterSalesOrderInfo, OrderMasterInfo orderMasterInfo, boolean isApply) throws OrderManagerException {
        String description = "买家申请售后；订单状态：" + getOrderStatusName(orderMasterInfo.getOrderStatus()) + "；售后类型：维修；" + getReason(afterSalesOrderInfo);
        if (!isApply) {
            description = "买家修改申请，售后类型：维修 " + getReason(afterSalesOrderInfo);
        }
        // 支持订单状态（待收货，已完成1年内）
        if (orderMasterInfo.getOrderStatus() == OrderStatusEnum.UN_COMMIT.getValue()
                || orderMasterInfo.getOrderStatus() == OrderStatusEnum.SUCCESS.getValue()) {
            // 检查售后原因是否匹配
            checkReason(orderMasterInfo.getOrderStatus(), afterSalesOrderInfo.getAfterSalesType(), afterSalesOrderInfo.getReasonType(), 365);
            // 已完成1年内判断
            Date now = new Date();
            checkExpired(now, orderMasterInfo, YEAR);
            // 查询当前售后类型是否被永久拒绝
            hasForeverRejected(orderMasterInfo, afterSalesOrderInfo);
            // 查询是否存在正在处理的售后申请
            hasUnFinishAfterSalesOrder(orderMasterInfo, afterSalesOrderInfo);
            // 查询此类型支持的售后最大件数
            checkApplyNumber(orderMasterInfo, afterSalesOrderInfo);
            // 插入进度
            setStep(now, afterSalesOrderInfo, description, 0);
            afterSalesOrderInfo.setAddressInfo(orderMasterInfo.getAddressInfo());
        } else {
            throw new OrderManagerException(OrderExceptionCodeCodeEnum.ORDER_STATUS_ERROR3);
        }
    }
}
